/*引用库*/
$.evalFile(File($.fileName).parent + "/lib/kersBoru_lib.jsx");
$.evalFile(File($.fileName).parent + "/lib/json2.jsx");
try {
    JSON
} catch (e) {
    $.writeln("1因为未载入 JSON 解析库，请载入 json2.jsx ");
}

var doc = app.activeDocument;
var configFile = new File("D:/Black8/bjj_cn/svn/ui/upload/T图标/配置表/索引_英灵头像.json");
/**
 * 
 * @returns 返回配置文件对象
 */
function configLine() {
    if (configFile.exists) {
        configFile.open("r"); //打开JSON文件
        var con_content = configFile.read(); //读取JSON内容
        var con_line = JSON.parse(con_content)[0];
    }
    return con_line;
}
/**
 * 主程序
 */
var main = function () {

    var con_line = configLine();
    var pat = con_line.local;
    var excelFile = new File(pat + con_line.json);

    if (excelFile.exists) {
        excelFile.open("r"); //打开JSON文件
        var content = excelFile.read(); //读取JSON内容
        var lines = JSON.parse(content);
        if (lines) {
            for (var i = 0; i < lines.length; i++) {
                var line = lines[i];
                //var yl = analyticYL(line);

                var folderName_0 = con_line.file[0];//正常
                var folderName_1 = con_line.file[1];//带主角 
                var folderName_2 = con_line.file[2];//小头像

                $.writeln("  ");
                modifyPSDcontent(line);
                
                saveImg(pat, folderName_2, line.id);
                //saveImg(pat, folderName_1, (line.id + 2));

            }
        }
    }

}/**
 * 
 * @param {*} yl 英灵对象
 * @param {*} qualityValue 当前英灵品质的颜色值
 * @param {*} qualityName  当前英灵品质 (绿,蓝,紫,橙,红,金)
 * @param {*} star 当前英灵星级
 * @param {*} dao  当前英灵道果(星魂)等级
 */
function modifyPSDcontent(yl) {

    var con_line = configLine();
    var layers = doc.layers;

    var imgPath = con_line.local + con_line.image + yl.name + ".png";

    //var job = yl.job;

    for (var i = 0; i < layers.length; i++) {
        var layer = layers[i];
        switch (layer.name) {
            case "图标资源": //替换 图标资源
                activeDocument.activeLayer = layer;
                kersBoru.listenerType.placedLayerReplaceContents(imgPath);
                break;
            default:
                { }
        }

    }



}
/**
 * 保存图片
 * @param {*} pat 链接对象的路径 目录
 * @param {*} type 图标类型
 * @param {*} imgName 图片名称
 */
function saveImg(pat, type, imgName) {

    var saveOption = new ExportOptionsSaveForWeb();
    saveOption.format = SaveDocumentType.PNG;
    saveOption.PNG8 = false;
    var path = pat + "正式名称1/" + type;
    var folder = new Folder(path);
    if (!folder.exists) {
        // alert("这里是folder.exists ");					//创建文件夹
        folder.create();
    }
    var outfile = new File(decodeURI(path + "/") + imgName + ".png"); //保存图片到
    //alert("这里是outfile   "+outfile);
    doc.exportDocument(outfile, ExportType.SAVEFORWEB, saveOption);
    //alert("这里是000000000000000   "); 
    $.writeln("保存");
}
main();